﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Lecturer --     

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<Lecturer> GetListLecturerByMajorId(int majorId)
        {
            return DataContex != null
                       ? (majorId > 0
                              ? DataContex.Lecturers.Where(m => m.IdMajor == majorId && m.Id != 1).ToList()
                              : DataContex.Lecturers.ToList())
                       : null;
        }

        public List<Lecturer> GetListLecturerQuality(int majorId)
        {
            return DataContex != null && majorId > 0
                        ? DataContex.Lecturers.Where(m => m.IdMajor == majorId && m.UniversityDegree != "Cử nhân" && m.Id != 1).ToList() : null;
        }

        #endregion

        #region Common

        private static bool LecturerCommonValidator(Lecturer lecturer, out string message)
        {
            if (string.IsNullOrWhiteSpace(lecturer.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Giảng viên");
                return false;
            }

            if (string.IsNullOrWhiteSpace(lecturer.UniversityDegree))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "học vị");
                return false;
            }

            if (lecturer.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Giảng viên", 50);
                return false;
            }

            message = string.Empty;
            return true;
        }

        #endregion

        #region -- Add Student --

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lecturer"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddLecturerValidator(Lecturer lecturer, out string message)
        {
            if (!LecturerCommonValidator(lecturer, out message)) return false;

            if (DataContex.Students.Any(f => f.Name.ToLower() == lecturer.Name.ToLower()))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, lecturer.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddLecturer(Lecturer lecturer)
        {
            DataContex.Lecturers.AddObject(lecturer);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Major --


        public bool EditLecturerValidator(Lecturer lecturer, out string message)
        {
            var id = lecturer.Id;

            if (!LecturerCommonValidator(lecturer, out message)) return false;

            if (DataContex.Lecturers.Any(m => m.Name.ToLower() == lecturer.Name.ToLower() && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, lecturer.Name);
                return false;
            }
          
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lecturer"></param>
        /// <returns></returns>
        public bool EditLecturer(Lecturer lecturer)
        {
            var id = lecturer.Id;
            var updateLecturer = DataContex.Lecturers.SingleOrDefault(f => f.Id == id);

            if (updateLecturer == null)
                return false;

            updateLecturer.Name = lecturer.Name;
            updateLecturer.IdMajor = lecturer.IdMajor;
            updateLecturer.DateOfBirth = lecturer.DateOfBirth;
            updateLecturer.UniversityDegree = lecturer.UniversityDegree;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Lecturer --

        public bool DeleteLecturersValidator(IEnumerable<Lecturer> lecturers, out string message)
        {
            const string intialize = "Giảng viên: ";
            var itemsCannotIdentify = (from lecturer in lecturers
                                       let id = lecturer.Id
                                       where !DataContex.Lecturers.Any(m => m.Id == id)
                                       select lecturer).Aggregate(intialize, (current, lecturer) => current + (lecturer.Name + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            var itemsInUse = (from lecturer in lecturers
                              let id = lecturer.Id
                              where DataContex.GroupLecturers.Any(m => m.IdLecturer == id) || DataContex.SubcommitteeLecturers.Any(m => m.IdLecturer == id)
                              select lecturer).Aggregate(intialize, (current, lecturer) => current + (lecturer.Name + ", "));

            if (itemsInUse != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsInUseCanNotDelete, itemsInUse.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteLecturers(List<Lecturer> lecturers)
        {
            for (var i = lecturers.Count - 1; i >= 0; i--)
            {
                var id = lecturers[i].Id;
                var deleteLecturer = DataContex.Lecturers.Single(m => m.Id == id);
                DataContex.Lecturers.DeleteObject(deleteLecturer);
            }
            DataContex.SaveChanges();
        }

        #endregion      
    }
}